package com.zhaishuaishuai.serviceCaller.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Controller("HystrixConsumerController")
@RequestMapping("/hystrix")
public class HystrixConsumerController {

    @Resource(name = "restTemplate")
    private RestTemplate restTemplate;

    //测试熔断器，调用provider里面的方法
    @HystrixCommand(fallbackMethod = "sayHelloHystrixMethod")
    @RequestMapping(value = "/sayHello",method = RequestMethod.POST)
    @ResponseBody
    public String sayHello(){
        String url = "http://localhost:10213/sayHello";
        return restTemplate.getForObject(url, String.class);
    }
    
    public String sayHelloHystrixMethod(){
        return "HystrixConsumerController熔断器开启";
    }
}
